package com.hqyj.am.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.am.dao.PowerMapper;
import com.hqyj.am.pojo.MyNode;
import com.hqyj.am.pojo.Power;
import com.hqyj.am.pojo.ResultVo;
import com.hqyj.am.service.IPowerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-08-25
 */
@Service
public class PowerServiceImpl extends ServiceImpl<PowerMapper, Power> implements IPowerService {
    //注入dao
    @Resource
    PowerMapper powerMapper;

    @Override
    public ResultVo queryByName(String userName) {
        System.out.println("userName"+userName);
        ResultVo vo = new ResultVo();
        //查询权限
        List<MyNode> list = powerMapper.queryByName(userName);
        System.out.println("list:"+ list);
        //创建权限菜单集合
        List<MyNode> listNew = new LinkedList<>();
        System.out.println("listNew"+listNew);
        if (list.size()>0){
            //构建树形菜单集合
            for(MyNode n:list){

                //查询出一级菜单
                if (n.getParentId()==0){
                    //创建一个子菜单集合
                    List<MyNode> listChildren = new LinkedList<>();

                    for(MyNode m:list){
                        if (m.getParentId()==n.getNodeId()){
                            //添加子菜单
                            listChildren.add(m);
                        }
                    }

                    //把子菜单集合添加到一级菜单
                    n.setChildren(listChildren);
                    //把父子关系构建好的菜单添加到新的权限菜单集合
                    listNew.add(n);
                }
            }
            vo.setData(listNew);
            vo.setSuccess(true);
            vo.setInfo("查询成功");
        }else {
            vo.setInfo("查询失败");
        }


        return vo;
    }
}
